package com.siddy.bbs.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedPreferenceManager {

	public static final String AUTHORITY = "com.siddy.bbs";

	public static final String ACCOUNT = "com.siddy.bbs.account";
	public static final String PASSWORD = "com.siddy.bbs.password";

	private SharedPreferences pref;

	private static SharedPreferenceManager prefManger;

	public static SharedPreferenceManager getSharedPreferenceManger() {
		if (prefManger == null) {
			throw new RuntimeException("SharedPrefenceManger has not been initialized");
		}
		return prefManger;
	}

	public static SharedPreferenceManager init(Context ctx) {
		prefManger = new SharedPreferenceManager(ctx);
		return prefManger;
	}

	private SharedPreferenceManager(Context ctx) {
		pref = ctx.getSharedPreferences(AUTHORITY, Context.MODE_PRIVATE);
	}

	public void writePref(String key, String value) {
		Editor edit = pref.edit();
		edit.putString(key, value);
		edit.commit();
	}

	public void writePref(String key, int value) {
		Editor edit = pref.edit();
		edit.putInt(key, value);
		edit.commit();
	}

	public void writePref(String key, boolean value) {
		Editor edit = pref.edit();
		edit.putBoolean(key, value);
		edit.commit();
	}

	public void writePref(String key, long value) {
		Editor edit = pref.edit();
		edit.putLong(key, value);
		edit.commit();
	}

	public void writePref(String key, float value) {
		Editor edit = pref.edit();
		edit.putFloat(key, value);
		edit.commit();
	}

	public int readInt(String key) {
		return pref.getInt(key, 0);
	}

	public boolean readBoolean(String key) {
		return pref.getBoolean(key, false);
	}

	public boolean readBoolean(String key, boolean def) {
		return pref.getBoolean(key, def);
	}

	public float readFloat(String key) {
		return pref.getFloat(key, 0F);
	}

	public long readLong(String key) {
		return pref.getLong(key, 0L);
	}

	public String readString(String key) {
		return pref.getString(key, null);
	}
}
